package com.ch5;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

 

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpHeaders.Values;
import io.netty.handler.codec.http.HttpRequest;

/**
 * @author U317076
 * 
 */
public class HttpServerHandler extends ChannelInboundHandlerAdapter {
	
	
	
	private HttpRequest request;
	
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelActive(ctx);
	    System.out.println(">>>> active");
	
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		// TODO Auto-generated method stub

		if (msg instanceof HttpRequest) {
			request = (HttpRequest) msg;

			String uri = request.getUri();
			System.out.println("Uri:" + uri);
		//	ctx.writeAndFlush("hello wrold");
		}
		
	     if (msg instanceof HttpContent) {
	            HttpContent content = (HttpContent) msg;
	            ByteBuf buf = content.content();
	            System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
	            buf.release();

	            String res = "I am OK";
	            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
	                    OK, Unpooled.wrappedBuffer(res.getBytes("UTF-8")));
	            response.headers().set(CONTENT_TYPE, "text/plain");
	            response.headers().set(CONTENT_LENGTH,
	                    response.content().readableBytes());
	            if (HttpHeaders.isKeepAlive(request)) {
	                response.headers().set(CONNECTION, Values.KEEP_ALIVE);
	            }
	            ctx.write(response);
	            ctx.flush();
	        }

	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		ctx.close();
	}

}
